package com.szf.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {
	private static final String DATE_FORMAT = "yyyy-MM-dd";
	private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static Date offsetMinute(Date date, int min) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, min);
		return calendar.getTime();
	}

	public static String getCurrent() {
		return new SimpleDateFormat(DATE_FORMAT).format(new Date());
	}

	public static String getFullMonth() {
		return getCurrent().substring(5, 7);
	}

	public static int getDateLength(final String startDate, final String endDate, String pattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		Date startDateTime = sdf.parse(startDate);
		Date endDateTime = sdf.parse(endDate);
		// 根据相差的毫秒数转换为天数
		int length = (int) ((endDateTime.getTime() - startDateTime.getTime()) / (1000 * 60 * 60 * 24));
		if (length >= 0) {
			length += 1;
		} else {
			length -= 1;
		}
		return length;
	}

	public static int getCurMinute() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MINUTE);
	}

	public static String getDate(long time) {
		try {
			Date date = new Date(time);
			return new SimpleDateFormat(DEFAULT_FORMAT).format(date);
		} catch (Exception e) {
			return new SimpleDateFormat(DEFAULT_FORMAT).format(new Date());
		}
	}

}
